package main

import (
    "fmt"
    "time"
)

func main(){
    //带有缓冲的channel
    c := make(chan int,3)

    go func(){
        defer fmt.Println("goroutine结束")

        //到channel存满会进行阻塞，等待空间
        for i := 0; i < 5; i++{
            c <- i
            fmt.Println("元素=",i,"len(c) = ",len(c),", cap(c) = ",cap(c))
        }
    }()

    time.Sleep(2 * time.Second)

    for i := 0; i < 5; i++{
        num := <-c
        fmt.Println("num = ",num)
    }

    fmt.Println("main goroutine 结束")
}